package com.xy.view;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;

import com.nineoldandroids.view.ViewHelper;

/**
 * Created by John on 2016/11/2.
 */

public class AlphaVIewpager extends ViewPager {
    private float MIN_SCALE = 0.3f;
    public AlphaVIewpager(Context context) {
        this(context,null);
    }

    public AlphaVIewpager(Context context, AttributeSet attrs) {
        super(context, attrs);
        setPageTransformer(true,new DepthPageTransformer());
    }

    class DepthPageTransformer implements PageTransformer{

        @Override
        public void transformPage(View page, float position) {
            int width = page.getWidth();
            if (position < -1){
                ViewHelper.setAlpha(page,0);
            }else if (position <=0){
                ViewHelper.setAlpha(page,1);
                ViewHelper.setTranslationX(page,0);
                ViewHelper.setScaleX(page,1);
                ViewHelper.setScaleY(page,1);
            }else if (position <=1){
                ViewHelper.setAlpha(page,1-position);
                ViewHelper.setTranslationX(page,width * -position);
                float scale = MIN_SCALE + (1-MIN_SCALE)*(1-Math.abs(position));
                ViewHelper.setScaleX(page,scale);
                ViewHelper.setScaleY(page,scale);
            }else {
                ViewHelper.setAlpha(page,0);
            }
        }
    }
}
